package com.ssyt.yt1.day24.statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.ssyt.yt1.common.Constant;

/**
 * 数据操作：增、删、改
 *
 * @author Hsu.
 * @since 2015年12月25日
 */
public class DMLDemo {

	/**
	 * 增、删、改
	 * 
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void test(String sql) throws ClassNotFoundException,
			SQLException {
		// 1、加载驱动
		Class.forName(Constant.DRIVER);

		// 2、连接数据库
		Connection connection = null;
		Statement statement = null;
		try {
			connection = DriverManager.getConnection(Constant.URL,
					Constant.USER, Constant.PASSWORD);
			// 3、操作数据
			statement = connection.createStatement();
			int row = statement.executeUpdate(sql);
			System.out.println("受影响的行数：" + row);
		} finally {
			// 4、关闭连接
			if (statement != null) {
				statement.close();
			}
			if (connection != null) {
				connection.close();
			}
		}
	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		String sql = "insert into user values(null, '5002281888096578', 'lisi', 'iloveyouzhangsan', 99999.99, null, null)";

		test(sql);
	}
}
